<%@ page import="model.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    // 获取用户信息和消息
    User user = (User)request.getAttribute("user");
    String success = (String)request.getAttribute("success");
    String error = (String)request.getAttribute("error");
    
    // 处理头像URL，确保有默认值
    String avatarUrl = user != null && user.getAvatar() != null ? user.getAvatar() : "https://via.placeholder.com/120";
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心 - 回声网络</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            color: #2c3e50;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        .profile-card {
            background: #f9f9f9;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .profile-avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            margin-bottom: 20px;
            border: 3px solid #3498db;
            object-fit: cover;
        }
        .avatar-container {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], input[type="email"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s;
        }
        .btn:hover {
            background: #2980b9;
        }
        .error-message {
            color: #e74c3c;
            margin-top: 10px;
        }
        .success-message {
            color: #2ecc71;
            margin-top: 10px;
        }
        .avatar-upload {
            display: inline-block;
            margin-top: 10px;
        }
        .avatar-upload input[type="file"] {
            display: none;
        }
        .upload-btn {
            background: #95a5a6;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .upload-btn:hover {
            background: #7f8c8d;
        }
    </style>
</head>
<body>
    <h1>个人中心</h1>
    
    <div class="profile-card">
        <h2>修改个人资料</h2>
        
        <!-- 显示成功或错误消息 -->
        <% if (success != null) { %>
            <div class="success-message"><%= success %></div>
        <% } %>
        <% if (error != null) { %>
            <div class="error-message"><%= error %></div>
        <% } %>
        
        <!-- 头像显示区域 -->
        <div class="avatar-container">
            <img class="profile-avatar" src="<%= avatarUrl %>" alt="用户头像">
        </div>
        
        <form action="profile" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="username">昵称</label>
                <input type="text" id="username" name="username" value="<%= user != null ? user.getUsername() : "" %>" required>
            </div>
            
            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" id="email" name="email" value="<%= user != null ? user.getEmail() : "" %>" required>
            </div>
            
            <div class="form-group">
                <label for="avatar">上传头像</label>
                <div class="avatar-upload">
                    <input type="file" id="avatar" name="avatar" accept="image/*">
                    <label for="avatar" class="upload-btn">选择图片</label>
                </div>
                <p style="font-size: 14px; color: #666; margin-top: 5px;">支持JPG、PNG格式，建议尺寸120x120</p>
            </div>
            
            <button type="submit" class="btn">保存修改</button>
        </form>
    </div>
    
    <div class="profile-card">
        <h2>修改密码</h2>
        <form action="profile/password" method="post">
            <div class="form-group">
                <label for="oldPassword">当前密码</label>
                <input type="password" id="oldPassword" name="oldPassword" required>
            </div>
            
            <div class="form-group">
                <label for="newPassword">新密码</label>
                <input type="password" id="newPassword" name="newPassword" required>
            </div>
            
            <button type="submit" class="btn">修改密码</button>
        </form>
    </div>
</body>
</html>